package com.buss.admin.controller;

import java.util.Map;

import com.buss.auth.transfer.vo.authen.AuthUserVO;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jweb.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.buss.common.service.impl.FileService;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/file")
public class FileController {

	@Autowired
	private FileService fileUploadService;

	@RequestMapping("/upload")
	@ResponseBody
	public Map fileUpload(@RequestParam MultipartFile uploadFile) {

		
		Map result = fileUploadService.uploadFile(uploadFile);
		return result;
	}


	@RequestMapping("/uploadmain")
	public void uploadmain(HttpServletRequest request)  throws Exception {
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (!isMultipart) {
//			this.state = this.errorInfo.get("NOFILE");
			System.out.println("NO FILE");
//			{"state": "\u4e0a\u4f20\u8868\u5355\u4e0d\u662fmultipart/form-data\u7c7b\u578b"}
			return;
		}

		String result = "";
		Map resultMap = null;

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();

		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile uploadFile = entity.getValue();// 获取上传文件对象
			try {
				resultMap = fileUploadService.uploadFile(uploadFile);
				System.out.println("resultMap:" + resultMap);
//				http://hongxinguoji.com/group1/M00/00/0E/Zckdh1c1k1eAJnH-AAA-XydFCOQ070.jpg
//				http://hongxinguoji.com/group1/M00/00/0E/
				result = resultMap.get("url").toString();
				System.out.println("result:" + result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 如果是单传，则只循环一次
			// break;
		}
	}

}
